/**
 * Copyright (c) 2005, 2017, EVECOM Technology Co.,Ltd. All rights reserved.
 * EVECOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.weplay.gameplane.utils;


import android.text.TextUtils;


import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateTimeUtils {

    /**
     * 默认格式 1
     */
    public static final String DEF_DATE_FORMAT_1 = "yyyy-MM-dd";
    /**
     * 默认格式 2
     */
    public static final String DEF_DATE_FORMAT_2 = "yyyy-MM-dd HH:mm";
    /**
     * 默认格式 3
     */
    public static final String DEF_DATE_FORMAT_3 = "yyyy-MM-dd HH:mm:ss";
    /**
     * 默认格式 4
     */
    public static final String DEF_DATE_FORMAT_4 = "HH:mm";
    /**
     * 默认格式 5
     */
    public static final String DEF_DATE_FORMAT_5 = "yyyy";
    /**
     * 默认格式 6
     */
    public static final String DEF_DATE_FORMAT_6 = "yyyyMMddHHmmss";

    /**
     * 默认格式 7
     */
    public static final String DEF_DATE_FORMAT_7 = "HH:mm:ss";

    /**
     * 默认格式 8
     */
    public static final String DEF_DATE_FORMAT_8 = "yyyy_MM_dd_HH_mm_ss";

    /**
     * 默认格式 9
     */
    public static final String DEF_DATE_FORMAT_9 = "MM-dd";

    /**
     * 默认格式 10
     */
    public static final String DEF_DATE_FORMAT_10 = "yyyy-MM";

    /**
     * 默认格式 11
     */
    public static final String DEF_DATE_FORMAT_11 = "yyyy年MM月dd日HH时mm分";
    /**
     * 默认格式 12
     */
    public static final String DEF_DATE_FORMAT_12 = "dd/MM月";

    /**
     * 默认格式 13
     */
    public static final String DEF_DATE_FORMAT_13 = "yyyy年";
    /**
     * 默认格式 14
     */
    public static final String DEF_DATE_FORMAT_14 = "HH";

    /**
     * 默认格式 15
     */
    public static final String DEF_DATE_FORMAT_15 = "yyyyMM";

    /**
     * 默认格式 15
     */
    public static final String DEF_DATE_FORMAT_16 = "yyyyMMdd";
    /**
     * 默认格式 15
     */
    public static final String DEF_DATE_FORMAT_17 ="yyyy年MM月dd日HH:mm";

    /**
     * 默认格式 3
     */
    public static final String DEF_DATE_FORMAT_18 = "yyyy-MM-dd HH:mm:ss:SSS";

    /**
     * 格式化 日期
     *
     * @param date
     * @param formatDate
     * @return
     */
    public static String formatTime(Date date, String formatDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatDate);
        return simpleDateFormat.format(date);
    }

    /**
     * 格式化 日期
     *
     * @param date
     * @param formatDate
     * @return
     */
    public static String formatTime(long date, String formatDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatDate);
        return simpleDateFormat.format(new Date(date));
    }

    /**
     * 格式化 日期
     *
     * @param
     * @return
     */
    public static String getCurrentTime() {
        return getCurrentDate(DEF_DATE_FORMAT_4);
    }


    /**
     * 格式化 日期
     *
     * @param
     * @return
     */
    public static String getCurrentDate() {
        return getCurrentDate("yyyy-MM-dd");
    }


    /**
     * 格式化 日期
     *
     * @param formatDate
     * @return
     */
    public static String getCurrentDate(String formatDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatDate);
        return simpleDateFormat.format(Calendar.getInstance().getTime());
    }

    /**
     * 格式化 日期
     *
     * @param
     * @return
     */
    public static long getCurrentDateLong() {
        return Calendar.getInstance().getTime().getTime();
    }


    /**
     * 时间戳转换成日期格式字符串
     *
     * @param stampStr 精确到秒的字符串
     * @param format
     * @return
     */
    public static String stampToDate(String stampStr, String format) {
        if (stampStr == null || stampStr.isEmpty() || stampStr.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(stampStr)));
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param stampStr
     * @param format
     * @return
     */
    public static String stampToDate(long stampStr, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(stampStr));
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param dateStr 字符串日期
     * @return
     */
    public static long dateToStamp(String dateStr) {
        try {
            if (TextUtils.isEmpty(dateStr)) {
                return 0;
            }
            String format = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateStr).getTime();
        } catch (Exception e) {
        }
        return 0;
    }


    /**
     * 日期格式字符串转换成时间戳
     *
     * @param dateStr 字符串日期
     * @param format  如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String dateToStamp(String dateStr, String format) {
        try {
            if (TextUtils.isEmpty(dateStr)) {
                return "";
            }
            if (format == null || format.isEmpty()) {
                format = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(dateStr).getTime());
        } catch (Exception e) {
        }
        return "";
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param dateStr 字符串日期
     * @param format  如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static long dateToStampForLong(String dateStr, String format) {
        try {
            if (TextUtils.isEmpty(dateStr)) {
                return 0;
            }
            if (format == null || format.isEmpty()) {
                format = "yyyy-MM-dd HH:mm:ss";
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateStr).getTime();
        } catch (Exception e) {
        }
        return 0;
    }


    /**
     * 格式化 日期
     *
     * @param date
     * @param formatDate
     * @return
     */
    public static Date stringChangeToDate(String date, String formatDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatDate);
        ParsePosition pos = new ParsePosition(0);
        return simpleDateFormat.parse(date, pos);
    }


    /**
     * 将 yyyy-MM-dd HH:mm:ss 转为 yyyy-MM-dd
     *
     * @param str
     * @return
     */
    public static String dateFormatChange(String str, String oldFormat, String newFormat) {
        try {
            if (StringUtils.isEmpty(str)) {
                return StringUtils.getString(str);
            }
            SimpleDateFormat oldSimpleDateFormat = new SimpleDateFormat(oldFormat);
            Date date = oldSimpleDateFormat.parse(str);

            SimpleDateFormat newSimpleDateFormat = new SimpleDateFormat(newFormat);
            return newSimpleDateFormat.format(date);
        } catch (ParseException e) {

        }
        return str;
    }

    /**
     * @description:获取当前月最后一天
     * @return 当前月最后一天
     * @author:
     * @createTime:2016年12月13日 下午4:28:58
     */
    public static String getMonthEnd() {
        return getMonthEnd(DateTimeUtils.DEF_DATE_FORMAT_1);
    }

    public static String getMonthEnd(String defFormat) {
        //获取当前月最后一天
        Calendar currentDate = Calendar.getInstance();
        currentDate.set(Calendar.DAY_OF_MONTH, currentDate.getActualMaximum(Calendar.DAY_OF_MONTH));

        if (StringUtils.isEmpty(defFormat)) {
            defFormat = "yyyy-MM-dd";
        }
        SimpleDateFormat format = new SimpleDateFormat(defFormat);
        return format.format(currentDate.getTime());
    }


    /**
     * 年
     *
     * @return
     */
    public static String getCurrentYearStr() {
        Calendar c = Calendar.getInstance();
        return getCurrentDate("yyyy");
    }


    /**
     * 年
     *
     * @return
     */
    public static int getCurrentYear() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR);
    }

    /**
     * 月
     *
     * @return
     */
    public static int getCurrentMonth() {
        Calendar c = Calendar.getInstance();
        return (c.get(Calendar.MONTH) + 1);
    }

    /**
     * 日
     *
     * @return
     */
    public static int getCurrentDay() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DATE);
    }

    /**
     * 时
     *
     * @return
     */
    public static int getCurrentHour() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 分
     *
     * @return
     */
    public static int getCurrentMinute() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MINUTE);
    }

    /**
     * 秒
     *
     * @return
     */
    public static int getCurrentSecond() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.SECOND);
    }

    /**
     * strTime要转换的String类型的时间
     * formatType时间格式
     * strTime的时间格式和formatType的时间格式必须相同
     */
    public static long stringToLong(String strTime, String formatType) {
        // String类型转成date类型
        Date date = stringToDate(StringUtils.getString(strTime, "0"), formatType);
        if (date == null) {
            return 0;
        } else {
            // date类型转成long类型
            long currentTime = dateToLong(date);
            return currentTime;
        }
    }

    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }


    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    /**
     * 返回文字描述的日期
     *
     * @param date
     * @return
     */
    public static String getTimeFormatText(Date date) {
        if (date == null) {
            return null;
        }
        long diff = new Date().getTime() - date.getTime();
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "个小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }


    /**
     * 格式化 迟到、早退时间
     *
     * @param time
     * @return
     */
    public static String formChiDaoZaoTui(int time) {
        int hour = time / 60;
        if (hour > 0) {
            int minute = time % 60;
            return String.format("%d小时%d分钟", hour, minute);
        } else {
            return String.format("%d分钟", time);
        }
    }


    /**
     * 格式化 迟到、早退时间
     *
     * @param time
     * @return
     */
    public static String formLongToString(long time) {
        int countTime = (int) (time / 1000);

        int hour = countTime / 3600;
        int minute = (countTime - (hour * 3600)) / 60;
        int second = countTime - (hour * 3600) - (minute * 60);

        String format = String.format("%s:%s",
                minute < 10 ? "0" + minute : minute + "",
                second < 10 ? "0" + second : second + "");

        return format;

    }

    /**
     * 结束时间 >= 开始时间
     *
     * @return
     */
    public static boolean endTimeBigStartTime(String startTime, String endTime, String timeFormat) {
        if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)){
            return true;
        }
        long startTimelong = dateToStampForLong(startTime, timeFormat);
        long endTimelong = dateToStampForLong(endTime, timeFormat);

        if ((endTimelong - startTimelong) >= 0) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 根据日期获取当前是星期几
     */
    public static int dateChangeToWeek(String date) {
        try {
            if (StringUtils.isEmpty(date)){
                return -1;
            }

            Calendar cal = Calendar.getInstance();
            SimpleDateFormat f = new SimpleDateFormat(DEF_DATE_FORMAT_1);
            Date d = f.parse(date);
            cal.setTime(d);
            //因为数组下标从0开始，而返回的是数组的内容，是数组{1,2,3,4,5,6,7}中用1~7来表示，所以要减1
            int weekNum = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (weekNum == 0){
                weekNum = 7;
            }
            return weekNum;

        } catch (ParseException e) {
        }
        return -1;
    }

    /**
     *
     * @param weekNum
     * @return
     */
    public static String weekNumToChinese(int weekNum){
        if (weekNum == -1){
            return "";
        }
        String[] weekChineseArray = new String[]{"", "一" , "二" , "三" , "四" , "五" , "六" , "日"};
        return weekChineseArray[weekNum];
    }

    /**
     * 检查是否是 一年的时间跨度 日期
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean checkDateIsSameYear(String startDate, String endDate) {
        return checkDateIsSameYear(startDate ,endDate, "");
    }

    /**
     * 检查是否是 一年的时间跨度 日期
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean checkDateIsSameYear(String startDate, String endDate,String format) {
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            return true;
        }
        long startLongTime = DateTimeUtils.dateToStampForLong(startDate, StringUtils.getString(format, "yyyy-MM-dd"));
        long endLongTime = DateTimeUtils.dateToStampForLong(endDate,  StringUtils.getString(format, "yyyy-MM-dd"));
        long countLongTime = endLongTime - startLongTime;
        float dayNum = countLongTime / (1000 * 60 * 60 * 24);

        if (dayNum <= 365) {
            //是否为一年的时间跨度
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检查是否是 一年的时间跨度 日期
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean checkDateTimePeriod(String startDate, String endDate,String format, int limitTimePeriod) {
        if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
            return true;
        }
        long startLongTime = DateTimeUtils.dateToStampForLong(startDate, StringUtils.getString(format, "yyyy-MM-dd"));
        long endLongTime = DateTimeUtils.dateToStampForLong(endDate,  StringUtils.getString(format, "yyyy-MM-dd"));
        long countLongTime = endLongTime - startLongTime;
        float dayNum = countLongTime / (1000 * 60 * 60 * 24);

        if (dayNum <= limitTimePeriod) {
            //是否为 时间跨度 内的时间段
            return true;
        } else {
            return false;
        }
    }

    /**

     * 获取本月的最后一天：

     * @return

     */

    public static String getFirstDayOfMonth(int year, int month) {

        SimpleDateFormat dateFormater = new SimpleDateFormat("dd");
        Calendar cal = Calendar.getInstance();
        // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        cal.set(Calendar.DAY_OF_MONTH, 1);
        return StringUtils.getString(dateFormater.format(cal.getTime()));

    }


    /**

     * 获取本月的第一天

     * @return

     */

    public static String getLastDayOfMonth(int year, int month) {
        SimpleDateFormat dateFormater = new SimpleDateFormat("dd");
        Calendar cal = Calendar.getInstance();
        // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return  StringUtils.getString(dateFormater.format(cal.getTime()));

    }


    /***
     * 获取昨天日期
     * @return
     */
    public static String getYesterday(){
        //获取前一天的日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        return stampToDate(time.getTime(), DEF_DATE_FORMAT_1);
    }

    /***
     * 获取 半年前的日期
     * @return
     */
    public static String getHalfAyearDate(){
        //获取前一天的日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -6);
        Date time = calendar.getTime();
        return stampToDate(time.getTime(), DEF_DATE_FORMAT_1);
    }



    /***
     * 获取昨天日期
     * @return
     */
    public static long getYesterdayLong(){
        //获取前一天的日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        Date time = calendar.getTime();
        return time.getTime();
    }

    public static Date stringToDate(String dateStr, String model) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(model, Locale.GERMANY);
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {

        }
        return date;
    }


}
